/**
 * Copyright (c) 2019 xstgongyi.org
 * All rights reserved.
 */
package org.xstgongyi.eduaid.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xstgongyi.eduaid.dao.StudentAidMapper;
import org.xstgongyi.eduaid.dataobject.ProjectPhaseDO;
import org.xstgongyi.eduaid.dataobject.StudentAidDO;
import org.xstgongyi.eduaid.bo.FundStatus;
import org.xstgongyi.eduaid.bo.ProjectPhaseStatus;
import org.xstgongyi.eduaid.bo.SponsorFeedbackStatus;
import org.xstgongyi.eduaid.bo.StudentAidStatus;
import org.xstgongyi.eduaid.bo.StudentFeedbackStatus;
import org.xstgongyi.eduaid.bo.StudentPairStatus;
import org.xstgongyi.eduaid.manager.ProjectPhaseManager;
import org.xstgongyi.eduaid.manager.SemesterManager;

/**
 * @author oldcrane<br/>
 * @created 2019-08-13
 */
@Service
public class StudentAidStatisticsServiceImpl implements StudentAidStatisticsService {

	@Autowired
	private StudentAidMapper aidDao;

	@Autowired
	private ProjectPhaseManager projectPhaseManager;

	@Autowired
	private SemesterManager semesterManager;

	@Override
	public List<StudentAidStatisticsDTO> calcStatisticsOfOngoingPhases(Integer projectId) {

		// get ongoing projects
		Map<String, Object> phaseCondition = new HashMap<>();
		phaseCondition.put("projectId", projectId);
		phaseCondition.put("status", ProjectPhaseStatus.ongoing);
		List<ProjectPhaseDO> phases = projectPhaseManager.list(phaseCondition);

		List<StudentAidStatisticsDTO> result = new ArrayList<>();
		Map<Integer, StudentAidStatisticsDTO> statMap = new HashMap<>();
		for (ProjectPhaseDO phase : phases) {
			StudentAidStatisticsDTO stat = new StudentAidStatisticsDTO();
			stat.setProjectId(phase.getProjectId());
			stat.setSemesterId(phase.getSemesterId());
			stat.setSemester(semesterManager.getById(phase.getSemesterId()));
			result.add(stat);
			statMap.put(phase.getSemesterId(), stat);
		}

		// retrieve aid records and count numbers
		Map<String, Object> aidCondition = new HashMap<>();
		aidCondition.put("projectId", projectId);
		aidCondition.put("offset", 0);
		aidCondition.put("limit", 999999);
		List<StudentAidDO> aidList = aidDao.listPage(aidCondition);

		for (StudentAidDO aid : aidList) {
			StudentAidStatisticsDTO stat = statMap.get(aid.getSemesterId());
			if (null == stat) {
				continue;
			}

			stat.setApplyTotal(stat.getApplyTotal() + 1);

			// 学生的资助进展
			if (StudentAidStatus.funded == aid.getAidStatus()) {
				stat.applyFunded++;
			} else if (StudentAidStatus.approved == aid.getAidStatus()) {
				stat.applyApproved++;
			} else if (StudentAidStatus.transferring == aid.getAidStatus()) {
				stat.applyTransferring++;
			} else if (StudentAidStatus.cancelled == aid.getAidStatus()) {
				stat.applyCancelled++;
			} else if (StudentAidStatus.registered == aid.getAidStatus()) {
				stat.applyUnknown++;
			}

			// 确定资助 TODO 是否需要增加这个条件？是否可以通过数据的控制，去掉这个条件？
			if (aid.getAidStatus().needAid()) {

				// 结对进展
				if (StudentPairStatus.donated == aid.getPairStatus()) {
					stat.pairDone++;
				} else if (StudentPairStatus.confirmed == aid.getPairStatus()) {
					stat.pairConfirmed++;
				} else if (StudentPairStatus.noNeed == aid.getPairStatus()) {
					stat.pairNoNeed++;
				} else if (StudentPairStatus.waitPair == aid.getPairStatus()) {
					stat.pairWait++;
				}

				// 助学金发放
				if (FundStatus.transfered == aid.getFundStatus()) {
					stat.fundTransfered++;
				} else if (FundStatus.waitTransfer == aid.getFundStatus()) {
					stat.fundNotStarted++;
				}

				// 学生收款证明收取和反馈
				if (StudentFeedbackStatus.collected == aid.getReceiptStatus()) {
					stat.receiptCollected++;
				}
				if (SponsorFeedbackStatus.completed == aid.getReceiptFeedbackStatus()) {
					stat.receiptSent++;
				} else if (SponsorFeedbackStatus.todo == aid.getReceiptFeedbackStatus()) {
					stat.receiptNeedSend++;
				}

				// 学期小结收取和反馈
				if (StudentFeedbackStatus.collected == aid.getSummaryStatus()) {
					stat.summaryCollected++;
				}
				if (SponsorFeedbackStatus.completed == aid.getSummaryFeedbackStatus()) {
					stat.summarySent++;
				} else if (SponsorFeedbackStatus.todo == aid.getSummaryFeedbackStatus()) {
					stat.summaryNeedSend++;
				}
			}
		}

		return result;
	}

}
